// pages/ucenter/login/login.js
var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var user = require('../../../utils/user.js');
var app = getApp();
var hostURL = app.globalData1.hostURL;
var ID = app.globalData2.userid;
var Phone = app.globalData2.userphone;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    motto: 'Hello World',
    phone: "",
    password: "",

  },
  /** 监听帐号输入 */
  listenerPhoneInput: function (e) {
    this.data.phone = e.detail.value;
  },
  /** 监听密码输入 */
  listenerPasswordInput: function (e) {
    this.data.password = e.detail.value;
  },

  register: function () {
    wx.navigateTo({
      url: '../register/register',
    })

  },
  forgetpassword: function () {

    wx.navigateTo({
      url: '../forgetpassword/forgetpassword',
    })
  },


  //登陆
  login: function () {
    var phone = this.data.phone;
    var password = this.data.password;
    var that = this;
    //  console.log(phone + "  " + password)
    if (phone === "") {
      wx.showToast({
        title: '用户名和密码不能为空',
        icon: 'loading',
        duration: 2000
      })
      return;
    }
    if (password === "") {
      wx.showToast({
        title: '用户名和密码不能为空',
        icon: 'loading',
        duration: 2000
      })
      return;
    }
    wx.request({
      method: "Post",
      url: hostURL + 'sharedplatform-web/user/login.action', //仅为示例，并非真实的接口地址
      data: ({
        phone: phone,
        password: password
      }),
      header: {

        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
      },

      success: function (res) {
        if (res.data.errno == 0) {
          app.globalData2.hasLogin = true;
          
          wx.setStorageSync("userInfo", res.data.userInfo)
          wx.setStorageSync('userInfo', res.data.userInfo);
          console.log(wx.getStorageSync('userInfo').nickName)
          console.log("变量id:" + res.data.userInfo.id);


          app.globalData2.userid = res.data.userInfo.id;
          app.globalData2.userphone = res.data.userInfo.phone
          console.log("id:" + app.globalData2.userid);


          console.log("AAAAA" + ID)

          wx.setStorage({
            key: "token",

            data: res.data.token,
            success: function () {
              wx.switchTab({
                url: '/pages/ucenter/index/index'
              });
            }
          });
        }
        else {
          that.setData({
            loginErrorCount: that.data.loginErrorCount + 1
          });
          app.globalData2.hasLogin = false;
          util.showErrorToast('账号或密码错误');
        }
      },




    })
  }
})
